<template>
  <div class="createPost-container">
    <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container" :inline="true"  label-width="120px" >

      <div class="createPost-main-container">
        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 电话号码:"   :required="true"  prop="mobile" >
              <el-input v-model="postForm.mobile"  :maxlength="25"  placeholder="请输入电话号码"   />
              <span> 比如, 13x12345678  </span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"  >
            <el-form-item  label="国际区号:" prop="area_code"  >
              <el-input  v-model="postForm.area_code" :maxlength="50"   placeholder="请输入国际区号" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12" >
            <el-form-item  label=" 商户名称:"  prop="selectedCustom">
              <el-select  :value="selectedCustom" @change="changeCustomer" >
                <el-option v-for="(item,index) in customerList" :key="item.id" :label="item.customerName" :value="item.customerName"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item   prop="customer_name_input" >
              <el-input  v-model="postForm.customer_name"  :maxlength="250"  placeholder="请输入商户名" />
              <span></span>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12" >
            <el-form-item  label="商户编码:" :required="true"  prop="customer_code"  >
              <el-input v-model="postForm.customer_code"  :maxlength="250"   placeholder="请输入商户编码"   />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item  label=" 一级仓库名:"  prop="selectedStorage">
              <el-select  :value="selectedStorage" @change="changeStorage" >
                <el-option v-for="(item, index) in storageList" :key="item.id" :label="item.storageName" :value="item.storageName"></el-option>
              </el-select>
            </el-form-item>

            <el-form-item  prop="storage_name">
              <el-input v-model="postForm.storage_name" :maxlength="250"   placeholder="请输入一级仓库名"     />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item  label=" 二级仓库名:"  prop="selectedRoom">
              <el-select  :value="selectedRoom" @change="changeRoom" >
                <el-option v-for="(item, index) in roomList" :key="item.id" :label="item.roomName" :value="item.roomName"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item  prop="room_name" >
              <el-input v-model="postForm.room_name" :maxlength="250"  placeholder="请输入二级仓库名"    />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"  >
            <el-form-item  label="dtu编码:" :required="true" prop="dtu_code"  >
              <el-input  v-model="postForm.dtu_code" :maxlength="250"   placeholder="请输入dtu编码" />
              <span> 比如, lenku2_100 </span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"  >
            <el-form-item  label="控制器编码:" :required="true" prop="control_code"  >
              <el-input  v-model="postForm.control_code" :maxlength="250"   placeholder="请输入控制器编码" />
              <span>比如, customer11</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12"  >
            <el-form-item  label="电表名:" :required="true" prop="meter_code"  >
              <el-input  v-model="postForm.meter_code" :maxlength="250"   placeholder="请输入电表名" />
              <span>比如, meter-100</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="4">
            <el-button type="primary" size="small" icon="el-icon-edit" @click="submitForm" style="margin-left:120px">提  交</el-button>

          </el-col>
          <el-col :span="2">
            <el-button  type="primary" size="small" icon="el-icon-lock" class="error" @click="resetForm">重  置</el-button>
          </el-col>
        </el-row>
      </div>
    </el-form>
  </div>
</template>

<script>
import Tinymce from '@/components/Tinymce'
import Upload from '@/components/Upload/singleImage3'
import MDinput from '@/components/MDinput'
import Sticky from '@/components/Sticky' // 粘性header组件
import { createStorageUnit } from '@/api/storage'
import { userSearch } from '@/api/remoteSearch'
import Warning from './Warning'
import { CommentDropdown, PlatformDropdown, SourceUrlDropdown } from '@/Components/Dropdown'
import {customerList} from "@/api/customer"
import {roomList} from "@/api/room"
import {storageList} from "@/api/storage"
const defaultForm = {
  mobile: '',
  dtu_code: '',
  room_name: '',
  customer_name: '',
  customer_code: '',
  storage_name: '',
  control_code: '',
  meter_code: ''
}

export default {
  name: 'StorageunitAdd',
  components: { Tinymce, MDinput, Upload, Sticky, Warning, CommentDropdown, PlatformDropdown, SourceUrlDropdown },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value === '') {
        if(rule.field == 'mobile'){
          callback(new Error('手机号码为必传项！'))
        }else if(rule.field == 'dtu_code'){
          callback(new Error('dtu编码为必传项！'))
        }else if(rule.field == 'customer_code'){
          callback(new Error('商户编码为必传项!'))
        }else if(rule.field == 'control_code'){
          callback(new Error('控制器编码为必传项!'))
        }else if(rule.field == 'meter_code'){
          callback(new Error('电表编码为必传项!'))
        }
        else{
          callback(new Error(rule.field + '为必传项!'))
        }
      } else {
        callback()
      }
    }

    return {
      postForm: Object.assign({}, defaultForm),
      loading: false,
      selectedCustom: '',
      selectedRoom: '',
      area_code:'',
      selectedStorage: '',
      userListOptions: [],
      customerList: [], //商户列表
      storageList: [],
      roomList: [],
      rules: {
        mobile: [{ validator: validateRequire }],

        customer_code: [{ validator: validateRequire }],

        dtu_code: [{ validator: validateRequire }],
        control_code: [{ validator: validateRequire }],
        meter_code: [{ validator: validateRequire }]
      }
    }
  },
  computed: {
    contentShortLength() {
      return this.postForm.content_short.length
    }
  },
  created() {
    this.fetchCustomData()
    this.fetchStorageData()

    if (this.isEdit) {
      //const id = this.$route.params && this.$route.params.id
      //this.fetchData(id)
    } else {
      this.postForm = Object.assign({}, defaultForm)
    }
  },
  methods: {
    changeCustomer(val){
      this.postForm.customer_name = this.selectedCustom = val
    },
    changeStorage(val){
      let changedStorageId = '';
      this.selectedRoom = '';
      this.postForm.room_name = '';
      for( let i = 0 ; i < this.storageList.length ; i++){
        if( this.storageList[i]['storageName'] == val){
          changedStorageId = this.storageList[i]['id']
        }
      }
      this.postForm.storage_name = this.selectedStorage = val
      this.fetchRoomData(changedStorageId)
    },
    changeRoom(val){
      console.log(val)
      this.postForm.room_name = this.selectedRoom = val
    },
    fetchCustomData(){
      customerList({ page: 1, limit: 1000 }).then(response => {
        this.customerList = response.data.data

      })
    },
    fetchStorageData(){
      storageList({ page: 1, limit: 1000 }).then(response => {
        this.storageList = response.data.data
      })
    },
    fetchRoomData(sid){

      roomList({ page: 1, limit: 1000, sid:sid }).then(response => {
        this.roomList = response.data.data

      })
    },

    fetchData(id) {
      // fetchArticle(id).then(response => {
      //   this.postForm = response.data
      // }).catch(err => {
      //   console.log(err)
      // })
    },
    resetForm() {
      this.$refs['postForm'].resetFields()
    },
    submitForm() {
      if(this.postForm.customer_name == ''){
        this.$message({ message:  '请填写商户名称!', type: 'error' })
        return
      }
      if(this.postForm.storage_name == ''){
        this.$message({ message:  '请填写一级仓库名称!', type: 'error' })
        return
      }
      if(this.postForm.room_name == ''){
        this.$message({ message:  '请填写二级仓库名称!', type: 'error' })
        return
      }
      this.$refs.postForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.doSubmitForm()
        } else {
          //this.$message({ message:  'error!', type: 'error' })
          return false
        }
      })
    },
    doSubmitForm() {
      // console.log(this.postForm)
      createStorageUnit(this.postForm).then(response => {
        if(response.data.code == 0){
          this.$message({ message:  '保存成功!', type: 'success' })
          this.resetForm()
        }else{
          this.$message({ message:  response.data.msg, type: 'error' })
        }
        this.listLoading = false
      })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/styles/mixin.scss";
.createPost-container {
  position: relative;
  .createPost-main-container {
    padding: 40px 45px 20px 50px;
    .postInfo-container {
      position: relative;
      @include clearfix;
      margin-bottom: 10px;
      .postInfo-container-item {
        float: left;
      }
    }
    .editor-container {
      min-height: 500px;
      margin: 0 0 30px;
      .editor-upload-btn-container {
        text-align: right;
        margin-right: 10px;
        .editor-upload-btn {
          display: inline-block;
        }
      }
    }
  }
  .word-counter {
    width: 40px;
    position: absolute;
    right: -10px;
    top: 0px;
  }
}
</style>
